FormValidator = {
	Require : /.+/,
	Email : /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/,
	Phone : /^((\(\d{3}\))|(\d{3}\-))?(\(0\d{2,3}\)|0\d{2,3}-)?[1-9]\d{6,7}$/,
	Mobile : /^((\(\d{3}\))|(\d{3}\-))?13\d{9}$/,
	Url : /^http:\/\/[A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\':+!]*([^<>\"\"])*$/,
	IdCard : /^\d{15}(\d{2}[A-Za-z0-9])?$/,
	Currency : /^\d+(\.\d+)?$/,
	//Number : /^\d+$/,
	NumberRex : /^\d+$/,
	Number : "this.numberL(value,getAttribute('length'))&&this.numberMinMax(value,getAttribute('min'),getAttribute('max'))",
	Zip : /^[1-9]\d{5}$/,
	QQ : /^[1-9]\d{4,8}$/,
	//Integer : /^[-\+]?\d+$/,
	IntegerRex : /^[-\+]?\d*$/,
	Integer : "this.integerL(value,getAttribute('length'))&&this.numberMinMax(value,getAttribute('min'),getAttribute('max'))",
	//Double : /^[-\+]?\d+(\.\d+)?$/,
	DoubleRex : /^[-\+]?\d*(\.\d*)?$/,
	Double : "this.doubleP(value,getAttribute('length'),getAttribute('precision'))&&this.numberMinMax(value,getAttribute('min'),getAttribute('max'))",
	English : /^[A-Za-z]+[0-9]*$/,
	Chinese : /^[\u0391-\uFFE5]+$/,
	UnSafe : /^(([A-Z]*|[a-z]*|\d*|[-_\~!@#\$%\^&\*\.\(\)\[\]\{\}<>\?\\\/\'\"]*)|.{0,5})$|\s/,
	IsSafe : function(str){return !this.UnSafe.test(str);},
	SafeString : "this.IsSafe(value)",
	Limit : "this.limit(value.length,getAttribute('min'), getAttribute('max'))",
	LimitB : "this.limit(this.LenB(value), getAttribute('min'), getAttribute('max'))",
	Date : "this.IsDate(value, getAttribute('format'))",
	DateTime : "this.IsDateTime(value,getAttribute('format'))",
	Repeat : "value == document.getElementsByName(getAttribute('to'))[0].value",
	Range : "getAttribute('min') < value && value < getAttribute('max')",
	Compare : "this.compare(value,getAttribute('operator'),getAttribute('to'))",
	Custom : "this.Exec(value, getAttribute('regexp'))",
	Group : "this.MustChecked(getAttribute('name'), getAttribute('min'), getAttribute('max'))",
	ErrorItem : [document.forms[0]],
	ErrorMessage : ["以下原因导致提交失败：\t\t\t\t"],
	execCommand: /^(\s|\S)*(exec(\s|\+)+(s|x)p\w+)(\s|\S)*$/,	
	imageXSS: /^(\s|\S)*((%3C)|<)((%69)|i|I|(%49))((%6D)|m|M|(%4D))((%67)|g|G|(%47))[^\n]+((%3E)|>)(\s|\S)*$/,
	scriptXSS: /^(\s|\S)*((%73)|s)(\s)*((%63)|c)(\s)*((%72)|r)(\s)*((%69)|i)(\s)*((%70)|p)(\s)*((%74)|t)(\s|\S)*$/,
	evalXSS: /^(\s|\S)*((%65)|e)(\s)*((%76)|v)(\s)*((%61)|a)(\s)*((%6C)|l)(\s|\S)*$/,
	validate : function(theForm, mode){
		
		var obj = theForm || event.srcElement;
		var count = obj.elements.length;
		
		this.ErrorMessage.length = 1;
		this.ErrorItem.length = 1;
		this.ErrorItem[0] = obj;
		for(var i=0;i<count;i++){
			with(obj.elements[i]){
				var defaultMsg;
				var _dataType = getAttribute("dataType");
				if(typeof(_dataType) == "object" || typeof(this[_dataType]) == "undefined") continue;
				this.ClearState(obj.elements[i]);
				if(getAttribute("require")== "true"){
					if(!this['Require'].test(value)){
						this.AddError(i, "必填项");//+getAttribute("msg")
						continue;
					}
				}
				if(getAttribute("require") == "false" && value == "") continue;
									
				switch(_dataType){
				    case "DateTime" :
					case "Date" :
					case "Repeat" :
					case "Range" :
					case "Compare" :
					case "Custom" :
					case "Group" : 
					case "Limit" :
					case "LimitB" :
					case "SafeString" :
					case "Integer":
					case "Number":
					case "Double":
						if(!eval(this[_dataType])) {
							inputErrorMsg=getAttribute("msg");
							if(typeof inputErrorMsg!="string"){
										if(_dataType=="Date")
											inputErrorMsg="日期不正确,应为年-月-日,如:1970-01-20";
										else if(_dataType=="Limit")
											inputErrorMsg="最大"+getAttribute("max")+"个字";
										else if(_dataType=="LimitB")
											inputErrorMsg="最大"+getAttribute("max")/2+"个汉字或"+getAttribute("max")+"个字母";
										else if(_dataType=="Integer"){
											inputErrorMsg="必须为整数";
											if(typeof getAttribute("length")=="string")
												inputErrorMsg+=",且最多"+getAttribute("length")+"位";
											if(typeof  getAttribute("max")=="string")
												inputErrorMsg+=",并且不能大于"+getAttribute("max");
											if(typeof getAttribute("min")=="string")
												inputErrorMsg+=",也不能小于"+getAttribute("min");
										}
										else if(_dataType=="Number"){
											inputErrorMsg="必须为数字";
											if(typeof getAttribute("max")=="string")
												inputErrorMsg+=",并且不能大于"+getAttribute("max");
											if(typeof getAttribute("min")=="string")
												inputErrorMsg+=",也不能小于"+getAttribute("min");
										}
										else if(_dataType=="Double"){
											inputErrorMsg="必须为数字,且最多";
											if(typeof getAttribute("precision")=="string")
												inputErrorMsg+=",且最多"+getAttribute("precision")+"位小数";
											if(typeof getAttribute("max")=="string")
												inputErrorMsg+=",并且不能大于"+getAttribute("max");
											if(typeof getAttribute("min")=="string")
												inputErrorMsg+=",也不能小于"+getAttribute("min");
										}
										else
											inputErrorMsg="格式不正确";
							}							
							this.AddError(i, inputErrorMsg);
						}
						break;
					default :
						if(!this[_dataType].test(value)){
							inputErrorMsg=getAttribute("msg");
							if(typeof inputErrorMsg!="string")
								inputErrorMsg=defaultMsg;
							this.AddError(i, inputErrorMsg);
						}
						break;
				}
			}
		}
		
		if(this.ErrorMessage.length > 1){
			mode = mode || 1;
			var errCount = this.ErrorItem.length;
			switch(mode){
				case 2 :
					for(var i=1;i<errCount;i++)
						this.ErrorItem[i].style.color = "red";
				case 1 :
					alert(this.ErrorMessage.join("\n"));
					this.ErrorItem[1].focus();
					break;
				case 3 :
					for(var i=1;i<errCount;i++){
						try{
							var span = document.createElement("SPAN");
							span.id = "__ErrorMessagePanel";
							span.style.color = "red";
							this.ErrorItem[i].parentNode.appendChild(span);
							span.innerHTML = this.ErrorMessage[i].replace(/\d+:/,"*");
						}
						catch(e){alert(e.description);}
					}
					this.ErrorItem[1].focus();
					break;
				default :
					alert(this.ErrorMessage.join("\n"));
					break;
			}
			return false;
		}
		return true;
	},
	//增加的实数校验，需要指定长度和小数位
	doubleP : function(value,len,precision){		
		if(value.indexOf("-")==0||value.indexOf("+")==0)  len=len+1;		
		if(this.DoubleRex.test(value)){
			dot=value.indexOf(".")		
			if(dot>-1){
				i=value.substring(0,dot);			
				p=value.substring(dot+1);
				return i.length<=len-precision&&p.length<=precision;
			}else{
				return value.length<=len-precision;
			}
		}
		return false;
	},
	//增加的数字型，可以通过length指定长度
	numberL : function(value,len){
		if(typeof len!='number')
			len=11;
		if(this.NumberRex.test(value))
			return value.length<len;
		return false;
	},
	//增加的整数型，可以通过length指定长度
	integerL : function(value,len){	
		if(typeof len!='number')
			len=11;
		if(value.indexOf("-")==0||value.indexOf("+")==0)  len=len+1;		
		if(this.IntegerRex.test(value))
			return value.length<=len;
		return false;
	},
	numberMinMax:function(value,min,max){		
		var ok=true;	
		if(typeof max!='undefined')
			ok=(value*1.0<=max);
		if(typeof min!='undefined')
			ok=ok&&(value*1.0>=min);
		return ok;
	},
	limit : function(len,min, max){
		min = min || 0;
		max = max || Number.MAX_VALUE;
		return min <= len && len <= max;
	},
	LenB : function(str){
		return str.replace(/[^\x00-\xff]/g,"**").length;
	},
	ClearState : function(elem){
		with(elem){
			if(style.color == "red")
				style.color = "";
			var lastNode = parentNode.childNodes[parentNode.childNodes.length-1];
			if(lastNode.id == "__ErrorMessagePanel")
				parentNode.removeChild(lastNode);
		}
	},
	AddError : function(index, str){
		this.ErrorItem[this.ErrorItem.length] = this.ErrorItem[0].elements[index];
		this.ErrorMessage[this.ErrorMessage.length] = this.ErrorMessage.length + ":" + str;
	},
	Exec : function(op, reg){
		return new RegExp(reg,"g").test(op);
	},
	compare : function(op1,operator,op2){
		switch (operator) {
			case "ne":
				return (op1 != op2);
			case "gt":
				return (op1 > op2);
			case "ge":
				return (op1 >= op2);
			case "lt":
				return (op1 < op2);
			case "le":
				return (op1 <= op2);
			default:
				return (op1 == op2); 
		}
	},
	MustChecked : function(name, min, max){
		var groups = document.getElementsByName(name);
		var hasChecked = 0;
		min = min || 1;
		max = max || groups.length;
		for(var i=groups.length-1;i>=0;i--)
			if(groups[i].checked) hasChecked++;
		return min <= hasChecked && hasChecked <= max;
	},
	IsDate:function(dateval,formatString){
    var arr = new Array();    
    if(dateval.indexOf("-") != -1){
        arr = dateval.toString().split("-");
    }else if(dateval.indexOf("/") != -1){
        arr = dateval.toString().split("/");
    }else{
        return false;
    }  
    switch(formatString){
	    case "yyyy-mm-dd" :
	    	m = dateval.match(new RegExp("^((\\d{4})|(\\d{2}))([-./])(\\d{1,2})\\4(\\d{1,2})$"));
			if(m == null && dateval != "") return false;
		    if(arr[0].length==4){
		        var date = new Date(arr[0],arr[1]-1,arr[2]);
		        if(date.getFullYear()==arr[0] && date.getMonth()==arr[1]-1 && date.getDate()==arr[2]){
		            return true;
		        }
		    }
    	case "dd-mm-yyyy" :
    		m = dateval.match(new RegExp("^(\\d{1,2})([-./])(\\d{1,2})\\2((\\d{4})|(\\d{2}))$"));
			if(m == null && dateval != "") return false;
		    if(arr[2].length==4){
		        var date = new Date(arr[2],arr[1]-1,arr[0]);
		        if(date.getFullYear()==arr[2] && date.getMonth()==arr[1]-1 && date.getDate()==arr[0]){
		            return true;
		        }
		    }    
    }
    return false;
	},
	IsDate_this_is_error_code : function(op, formatString){
		formatString = formatString || "yyyy-mm-dd";
		var m, year, month, day;
		switch(formatString){
			case "yyyy-mm-dd" :
				m = op.match(new RegExp("^((\\d{4})|(\\d{2}))([-./])(\\d{1,2})\\4(\\d{1,2})$"));
				if(m == null && op != "") return false;
				if(op != "") {
					day = m[6];
					month = m[5]--;
					year = (m[2].length == 4) ? m[2] : GetFullYear(parseInt(m[3], 10));
				}
				break;
			case "dddd-mm-yyyy" :
				m = op.match(new RegExp("^(\\d{1,2})([-./])(\\d{1,2})\\2((\\d{4})|(\\d{2}))$"));
				if(m == null && op != "") return false;
				if(op != "") {
					day = m[1];
					month = m[3]--;
					year = (m[5].length == 4) ? m[5] : GetFullYear(parseInt(m[6], 10));
				}
				break;
			default :
				break;
		}
		if(!parseInt(month) && op != "") return false;
		if(op != ""){
			month = month==12 ?0:month; 
			var date = new Date(year, month, day);
			return ((typeof(date) == "object" && year == date.getFullYear() && month == date.getMonth() && day == date.getDate()) || op == "");
		}
		return true;
		function GetFullYear(y){return ((y<30 ? "20" : "19") + y)|0;}
	},	
	//验证格式为 2008-08-08 20:00:00
	IsDateTime : function (str, op) {	    
		var reg = /^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2}) (\d{1,2}):(\d{1,2}):(\d{1,2})$/;		
		var r = str.match(reg); 
		if(r == null && str != "")return false;		
		//var d= new Date(r[1], r[3]-1,r[4],r[5],r[6],r[7]);
		var d;
		if(str != ""){
		  d= new Date(r[1], r[3]-1,r[4],r[5],r[6]);
		  return ((d.getFullYear()==r[1]&&(d.getMonth()+1)==r[3]&&d.getDate()==r[4]&&d.getHours()==r[5]&&d.getMinutes()==r[6]));
		}
		return true;
    },
    //验证格式如 2008-08-08 20:00		
	IsShortDateTime : function (str, op) {	    		
		var reg = /^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2}) (\d{1,2}):(\d{1,2})$/;  
		var r = str.match(reg); 
		if(r == null && str != "")return false;				
		var d;
		if(str != ""){
		  d= new Date(r[1], r[3]-1,r[4],r[5],r[6]);
		  return ((d.getFullYear()==r[1]&&(d.getMonth()+1)==r[3]&&d.getDate()==r[4]&&d.getHours()==r[5]&&d.getMinutes()==r[6]));
		}
		return true;
    }
}